import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 二叉树的实现
 * User: 74646
 * Date: 2022-03-02
 * Time: 14:55
 */
class TreeNode1{
    public char val; //数据
    public TreeNode1 left; //左孩子的引用
    public TreeNode1 right; //右孩子的引用
    public TreeNode1(char  val){
        this.val = val;
    }
}

public class BinaryTree {
    public TreeNode1 root; //二叉树的根节点

    public TreeNode1 creatTree1() {

        TreeNode1 nodeA = new TreeNode1('A');
        TreeNode1 nodeB = new TreeNode1('B');
        TreeNode1 nodeC = new TreeNode1('C');
        TreeNode1 nodeD = new TreeNode1('D');
        TreeNode1 nodeE = new TreeNode1('E');
        TreeNode1 nodeF = new TreeNode1('F');
        TreeNode1 nodeG= new TreeNode1('G');
        TreeNode1 nodeH = new TreeNode1('H');
        root = nodeA;
        nodeA.left = nodeB;
        nodeA.right = nodeC;
        nodeB.left = nodeD;
        nodeB.right = nodeE;
        nodeC.left = nodeF;
        nodeC.right = nodeG;
        nodeE.right = nodeH;
//        TreeNode1 nodeA = new TreeNode1(1);
//        TreeNode1 nodeB = new TreeNode1(2);
//        root = nodeA;
//        nodeA.left = nodeB;
        return root;
    }

//    public TreeNode1 creatTree2() {
//
//        TreeNode1 nodeA = new TreeNode1(1);
//        TreeNode1 nodeB = new TreeNode1(2);
//        root = nodeA;
//        nodeA.right = nodeB;
//        return root;
//    }

    //前序遍历
    void preOrder(TreeNode1 root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");

        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrde(TreeNode root){
        if(root==null){
            return;
        }
        postOrde(root.left);
        postOrde(root.right);
        System.out.print(root.val+" ");
    }

    /**
     * 就是二叉树结点的个数
     * @param root
     * @return
     */
    int size1(TreeNode root){
        int count = 0;
        if(root==null){
            return 0;
        }
        count+=1;
        count +=size1(root.left);
        count+=size1(root.right);
        return count;
    }

    int size(TreeNode root){
        if(root==null){
            return 0;
        }
        return size(root.left)+size(root.right)+1;
    }


    /**
     * 获取叶子节点的个数
     * @param root
     * @return 遍历
     */
    int count = 0;
    int getLeafNodeCount1(TreeNode root){
        if(root==null){
            return 0;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        if(root.left==null && root.right==null){
            count++;
        }
        return count;
    }

    /**
     * 获取叶子节点的个数，子问题思路
     * @param root
     * @return
     */
    int getLeafNodeCount(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null && root.right==null){
            //当前root为叶子结点
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root==null || k<=0){
            return 0;
        }
        if(k==1){
            return 1;
        }
       return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    //获取二叉树的高度
    int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
//        return  getHeight(root.left)>getHeight(root.right)?getHeight(root.left)+1:getHeight(root.right)+1;
    }

    //检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode ret = find(root.left,val);
        if(ret!=null){
            return ret;
        }
        ret = find(root.right,val);
        if(ret!=null){
            return ret;
        }
        return null;
    }

//    TreeNode find(TreeNode root, char val){
//        if(root==null){
//            return null;
//        }
//        if(root.val==val){
//            return root;
//        }
//        TreeNode ret = find(root.left,val);
//        if(ret!=null){
//            return ret;
//        }else {
//            return find(root.right,val);
//        }
//    }

    //判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
    while (!queue.isEmpty()){
        TreeNode top = queue.poll();
        if(top!=null){
            return false;
        }
    }
    return true;
}

//    boolean isCompleteTree(TreeNode root){
//        if(root==null){
//            return true; //空树也是一棵完全二叉树
//        }
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//        while (!queue.isEmpty()) {
//            TreeNode cur = queue.poll();
//            if (cur != null) {
//                queue.offer(cur.left);
//                queue.offer(cur.right);
//            } else {
//                break;
//            }
//        }
//        while (!queue.isEmpty()){
//            TreeNode top = queue.peek();
//            if(top!=null){
//                return false;
//            }
//            queue.poll();
//        }
//        return true;
//    }


    public boolean isSameTree(TreeNode1 p, TreeNode1 q) {
        if(p==null && q==null){
            return true;
        }
        if(p!=null && q==null){
            return false;
        }
        if(p==null && q!=null){
            return false;
        }

        if(p.val!=q.val){
            return false;
        }
        isSameTree(p.left,q.left);
        isSameTree(p.right,q.right);
        return true;
    }

    /**
     * 层序遍历
     * @param root 根节点
     */
    public void levelOrder1(TreeNode1 root){
        Queue<TreeNode1> queue = new LinkedList<>();
        if(root==null){
            return;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode1 cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }
//    public void levelOrder(TreeNode1 root){
//        Queue<TreeNode1> queue = new LinkedList<>();
//        if(root==null){
//            return;
//        }
//        queue.offer(root);
//        while (!queue.isEmpty()){
//            TreeNode1 cur = queue.poll();
//            if(cur!=null){
//                System.out.print(cur.val+" ");
//                queue.offer(cur.left);
//                queue.offer(cur.right);
//            }
//        }
//    }

    public List<List<Character>> levelOrder(TreeNode1 root) {
        List<List<Character>> ret = new ArrayList<>();
        Queue<TreeNode1> queue = new LinkedList<>();
        if(root==null){
            return ret;
        }
        while (!queue.isEmpty()){
            queue.offer(root);
            int size = queue.size(); //这个值表示当前层结点的个数
            List<Character> list = new ArrayList<>();
            while (size!=0){
                TreeNode1 cur = queue.poll();
                list.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

}
